<?php
namespace Swiftx\Bundle\WeChat\Controller\Company;
use Swiftx\Bundle\WeChat\Interfaces\Common\BaseController;
use Swiftx\Bundle\WeChat\Interfaces\Controller\Company\Layout as LayoutInterface;
use Swiftx\Bundle\WeChat\Interfaces\Controller\Company\Login as LoginInterface;
use Swiftx\Bundle\WeChat\Factory\Company as CompanyFactory;
use Swiftx\Bundle\WeChat\Interfaces\View\Seller as LayoutView;
use Swiftx\Bundle\WeChat\Interfaces\View\Login as LoginView;
use Swiftx\Bundle\WeChat\View\Api as ApiView;
use Swiftx\Interfaces\Application\Server;
use Swiftx\Interfaces\Httpd\Request;
use Swiftx\Interfaces\Httpd\Response;

/**
 * 用户登录页面实现
 */
class Layout extends BaseController implements LayoutInterface {

    /**
     * 企业工厂实例
     * @var CompanyFactory
     * @auto-injection
     */
    protected $companyFactory;

    /**
     * 系统服务对象
     * @var Server
     * @auto-injection
     */
    protected $server;

    /**
     * 接口视图对象
     * @var ApiView
     * @auto-injection
     */
    protected $apiView;

    /**
     * 显示框架页面
     * @param Request $request
     * @param Response $response
     * @router method   get
     * @return bool
     */
    public function showPage(Request $request, Response $response):bool {
        // 获取当前用户
        $company = $this->companyFactory->currentLogin($request);
        // 用户未登录
        if($company === null){
            /** @var LoginView $page */
            $page = $this->container->fetch(LoginView::class);
            $page->setTitle('微商城系统 | 登陆');
            $page->setLogoTitle('微商城');
            // 设置请求处理
            $dispatcher = $this->router->getDispatcher(
                LoginInterface::class,
                'actionVerifyIdentity'
            );
            $page->setAction($dispatcher->uri());
            // 设置成功转向
            $dispatcher = $this->router->getDispatcher(
                LayoutInterface::class,
                'showPage'
            );
            $page->setSuccessPage($dispatcher->uri());
            // 设置成功转向
            $dispatcher = $this->router->getDispatcher(
                LoginInterface::class,
                'showVerifyImage'
            );
            $page->setVerifyImageUrl($dispatcher->uri());
        }
        // 用户已登陆
        else {
            /** @var LayoutView $page */
            $page = $this->container->fetch(LayoutView::class);
            $page->setTitle('微商城系统 | 主页');
            $dispatcher = $this->router->getDispatcher(
                LayoutInterface::class,
                'apiRouterJs'
            );
            $page->setRouterApiUrl($dispatcher->uri());
            $page->setLogin($company);
        }
        // 渲染输出页面
        $response->render($page);
        return true;
    }

    /**
     * API路由接口
     * @param Request  $request
     * @param Response $response
     * @router method  get
     * @return bool
     */
    public function apiRouterJs(Request $request, Response $response):bool {
        // 项目路由配置
        $config = $this->server->parsePath('config/api.ini');
        $config = parse_ini_file($config, true);
        // 构建接口数组
        foreach ($config as &$node){
            foreach ($node as &$item){
                $temp = explode('::',$item);
                $dispatcher = $this->router->getDispatcher($temp[0],$temp[1]);
                $item = [
                    'url' => $dispatcher->uri(),
                    'method' => $dispatcher->getMethod()
                ];
            }
        }
        // 渲染输出页面
        $this->apiView->setConfig($config);
        $response->render($this->apiView);
        return true;
    }

    /**
     * 执行安全退出
     * @param Request $request
     * @param Response $response
     * @return bool
     */
    public function actionLogout(Request $request, Response $response): bool
    {
        // TODO: Implement actionLogout() method.
    }

    /**
     * 执行用户锁定
     * @param Request $request
     * @param Response $response
     * @return bool
     */
    public function actionLock(Request $request, Response $response): bool
    {
        // TODO: Implement actionLock() method.
    }

    /**
     * 定时消息同步
     * @param Request $request
     * @param Response $response
     * @return bool
     */
    public function timerStack(Request $request, Response $response): bool
    {
        // TODO: Implement timerStack() method.
    }


}